<template>
    <div>
        <div class="box">
            <van-action-sheet v-model="action_show">
                <h4 style="margin: 10px">为给您提供更好的服务</h4>
                <p style="margin: 10px">
                    我们将通过《服务协议》、《隐私政策》和《商家隐私声明》帮助您了解我们收集、使用、存储和共享个人信息的情况
                </p>
                <van-button type="primary" style="width: 90%; border-radius: 25px; margin: 40px 15px 20px 15px"
                    @click="tydl">同意并继续
                </van-button>
                <van-button style="width: 90%; border-radius: 25px; margin: 0 15px" @click="action_show = false">取消
                </van-button>
            </van-action-sheet>
            <van-action-sheet v-show="action_phone" v-model="action_phone">
                <van-form @submit="onSubmit" style="margin-top: 20px">
                    <van-field v-model="actphone" name="phone" label="绑定手机号" placeholder="请输入手机号"
                        :rules="action_TelRules" />
                    <div style="margin: 50px 16px">
                        <van-button round block type="info" native-type="submit">提交</van-button>
                    </div>
                </van-form>
            </van-action-sheet>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
export default {
    data() {
        return {
            actphone: '',
            action_phone: false,
            action_show: false,
            action_TelRules: [
                {
                    required: true,
                    message: '手机号不能为空',
                    trigger: blur
                },
                {
                    validator: (value) => {
                        return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(value);
                    },
                    message: '请输入正确的手机号格式',
                    trigger: 'onBlur'
                },
                {
                    validator: async () => {
                        let res = await this.$frontaxios.post(this.$front.postPhoneVerify, { phone: this.actphone });
                        if (res.code == 1) {
                            return true;
                        } else {
                            return false;
                        }
                    },
                    message: '手机号已注册',
                    trigger: 'onBlur'
                }
            ],
            randcode: ""
        };
    },
    methods: {
        async tydl() {
            let domainname = Object.keys(this.$route.params).length > 0 ? this.$route.params.domainname : localStorage.getItem('domainname');
            console.log('tydl',domainname,this.$route);
            const nexturl = window.location.href; //跳转到下一个页面的地址http://192.168.2.11/#/xuejing/order/test1?id=8
            console.log(nexturl);
            let url = this.$front.getWxUserInfo + `?nexturl=${encodeURIComponent(nexturl)}&domainname=${domainname}`; //请求获得openid并存入到服务器的cache当中
            console.log(url)
            window.location.href = url;
        },
        //注册
        async onSubmit(values) {
            let res = await this.$frontaxios.post(this.$front.updateRegPhone, { phone: values.phone, randcode: this.randcode });
            if (res.code == 1) {
                this.action_show = false;
                this.action_phone = false;
                this.$dialog.alert({ title: "温馨提示", message: "注册成功了！" });
                setTimeout(() => {
                    location.reload();
                }, 500);
            } else {
                this.$dialog.alert({ title: "温馨提示", message: "注册失败了，请刷新后再试！" });
            }
        }, async getData() {
            //在微信客户端里才能获取到微信openid
            let rst = Cookies.get('fronttoken');
            this.randcode = this.$route.query.randcode;
            let logintag = sessionStorage.getItem('logintag') ? sessionStorage.getItem('logintag') : 0;
            if ((this.randcode == undefined&&logintag==0)||(rst==undefined&&this.randcode == undefined)) {//没有登录就弹出登录协议提示窗口
                this.action_show = true;
                return;
            }
           // alert(this.randcode);
            if (this.randcode != undefined) {
                //如果没有获取到randcode也不执行获取用户数据
                let res = await this.$frontaxios.post(this.$front.getRandCodeUserInfo, { randcode: this.randcode });
                if (res.code == 1) {
                    //如果已经注册
                    this.userinfo = res.data;
                    // if (this.userinfo.phone) {
                        //说明已经有手机号了，直接登录
                        Cookies.set('fronttoken', res.data.token);
                        Cookies.set('frontrefreshtoken', res.data.refreshtoken);
                        localStorage.setItem('fronttoken', res.data.token);
                        localStorage.setItem('frontrefreshtoken', res.data.refreshtoken);
                        localStorage.setItem('phone', res.data.phone);
                        this.$notify({ type: 'success', message: '登录成功' });
                        this.show = false;
                        this.action_show = false;
                        sessionStorage.setItem('logintag',1);
                        this.$emit('loginData', 1);
                    // } else {
                        //没有手机号则提示绑定手机号
                        // this.action_phone = true;
                    // }
                } else if (res.code == 3) {//没有微信的老用户正在登录
                    // this.action_phone = true;
                } else if (res.code == 2) { //新用户正在登录，没注册过
                    this.action_phone = true;
                } else {
                    //可能是非法登录，提示重新登录
                    this.show = false;
                    this.action_show = true;
                    this.$dialog.alert({ title: '温馨提示', message: '获取登录数据失败，请稍后再试' });
                }
            }
        }
    },
    //根据判断本地是否有token值 来控制显示登录窗口
    async mounted() {
        if (isWeixin()) {
            let loginflag = sessionStorage.getItem('loginflag') ? sessionStorage.getItem('loginflag') : 0;
            let logintag = sessionStorage.getItem('logintag') ? sessionStorage.getItem('logintag') : 0;
            if(logintag==1){//已经登录
                this.$emit('loginData', 1);
                return;
            } 
            if (loginflag == 1) {//需要显示登录
                this.getData();
            }
        }
    }
};
</script>

<style lang="scss" scoped>
@import '~@/components/common/css/common.css';

.van-action-sheet {
    height: 250px;
}
</style>